<?php

class WPSPO_Object {
    
    var $errors = array();
    
    function wpspo_listprojetsByUser($idUser) {
        global $wpdb;
        if($idUser) {
                $listeProjets = get_posts( array(
                    'post_type'   => 'projet',
                    'numberposts' => 10, 
                    'orderby'     => 'post_date',
                    'post_parent' => 0,
                    'post_status' => 'publish',
                    'order'       => 'DESC',
                    'author' => $idUser
                    ) );

                if ( count( $listeProjets ) > 0 ) {

                    foreach ( $listeProjets as $listeProjet ) {
                           $liste .=  ''.$listeProjet->post_title.'<br />';
                    }
                }
                return $liste;
                
            } else {
                
                return 'Aucun utilisateur sélectionné';
            }
    }
    
    function wpspo_getListProjets() {
        global $wpdb;
        $listeProjets = get_posts( array(
            'post_type'   => 'projet',
            'numberposts' => 10, 
            'orderby'     => 'post_date',
            'post_parent' => 0,
            'post_status' => 'publish',
            'order'       => 'DESC',
            'author' => $idUser
            ) );

        if ( count( $listeProjets ) > 0 ) {

            return $listeProjets;
        }
        

    }
    
    function wpspo_getPostBytId($id, $postType = 'projet') {
        $args=array(
          'post_type' => $postType,
          'post_status' => 'publish',
          'ID' => $id
        );
        $my_posts = get_posts($args);
        if( $my_posts ) {
            return $my_posts;
        }
    }
    
    function wpspo_return_wpdb($postType = 'projet', $iduser = '', $postsPerPage = 10, $postStatus = 'publish') {
        global $wpdb;
        if($iduser) {
            $listeWpdb = get_posts( array(
                'post_type'   => $postType,
                'numberposts' => $postsPerPage, 
                'orderby'     => 'post_date',
                'post_parent' => 0,
                'post_status' => $postStatus,
                'order'       => 'DESC',
                'author' => $iduser
                ) );

            if ( count( $listeWpdb ) > 0 ) {

                foreach ( $listeWpdb as $listeRes ) {
                       $liste .=  ''.$listeRes->post_title.'<br />';
                }
            }
            return $liste;
        } else {
            return 'Aucun utilisateur sélectionné';
        }
    } 
    
    function updateStatus($id, $status) {
        $updatePost['post_status'] = $status; 
        if(wp_update_post( $updatePost )) {
            return true;
        }
    }
    
    function deleteMetaKey($postID) {
        
        global $wpdb;
        $delete = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s ", $postID, 'wpspo_choixprojets' ) );
        if($delete) {
            return true;
        }
    }
    
    function deletePostType($postID) {
        if(wp_delete_post($postID)) {
            return true;
        }
    }
} 